package com.example.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.example.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.example.gulimall.product.entity.AttrEntity;
import com.example.gulimall.product.service.AttrAttrgroupRelationService;
import com.example.gulimall.product.service.AttrService;
import com.example.gulimall.product.vo.AttrGroupWithAttrsRespVo;
import com.example.gulimall.product.vo.AttrVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.gulimall.common.utils.PageUtils;
import com.example.gulimall.common.utils.Query;

import com.example.gulimall.product.dao.AttrGroupDao;
import com.example.gulimall.product.entity.AttrGroupEntity;
import com.example.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
@Autowired
private AttrAttrgroupRelationService attrAttrgroupRelationService;
@Autowired
private AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long cateLogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>();
        if(!StringUtils.isEmpty(key)){
            queryWrapper.and((obj->{
                obj.lambda().like(AttrGroupEntity::getAttrGroupName,key).or().eq(AttrGroupEntity::getAttrGroupId,key);
            }));
        }
        //id为0，查询所有分类属性
        if(cateLogId==0){
            //this.page两个参数 第一参数为查询页码信息，其中Query.getPage方法传入map,自动封装为Ipage
            //第二个参数为查询条件
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);
            return new PageUtils(page);
        }
        queryWrapper.lambda().eq(AttrGroupEntity::getCatelogId,cateLogId);
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }

    @Override
    public void removeAttrGroupByIds(List<Long> attrGroups) {
        for (Long attrGroup : attrGroups) {
            this.removeById(attrGroup);
            LambdaQueryWrapper<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntityLambdaQueryWrapper = new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrGroup);
            attrAttrgroupRelationService.remove(attrAttrgroupRelationEntityLambdaQueryWrapper);
        }

    }

    @Override
    public List<AttrGroupWithAttrsRespVo> getAttrGroupsWithAttrs(Long catalogId) {
        LambdaQueryWrapper<AttrGroupEntity> attrGroupEntityLambdaQueryWrapper = new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCatelogId, catalogId);
        List<AttrGroupEntity> attrGroupEntityList = this.list(attrGroupEntityLambdaQueryWrapper);
        List<AttrGroupWithAttrsRespVo> attrGroupWithAttrsRespVoList = attrGroupEntityList.stream().map((attrGroup -> {
            AttrGroupWithAttrsRespVo attrGroupWithAttrsRespVo = new AttrGroupWithAttrsRespVo();
            BeanUtils.copyProperties(attrGroup, attrGroupWithAttrsRespVo);
            List<AttrEntity> relationAttr = attrService.getRelationAttr(attrGroup.getAttrGroupId());
            attrGroupWithAttrsRespVo.setAttrs(relationAttr);
            return attrGroupWithAttrsRespVo;
        })).collect(Collectors.toList());
        return attrGroupWithAttrsRespVoList;
    }


}
